/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-1-4 21:53:17
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#include "conclient.h"
#include <vector>

int g_blocksize; //消息块大小
int g_con_counts;  //并发连接个数

net_reactor_t g_reactor_engine;

int main(int argc, char* argv[])
{
    const char* ip = NULL;
    int port = 0;
    if(argc > 3) {
        ip = argv[1];
        port = atoi(argv[2]);
        g_blocksize = atoi(argv[3]);
        g_con_counts = argc > 4 ? atoi(argv[4]) : 1;
        
    } else {
        fprintf(stderr, "Usage: %s server_ip server_port block_size", argv[0]);
        fprintf(stderr, " [g_con_counts]\n"); 
        return -1;
    }
    
    if(port <= 0 || port >= 65536)
    {
        fprintf(stderr, "Usage: %s server_ip server_port block_size", argv[0]);
        fprintf(stderr, " [g_con_counts]\n"); 
        return -1;
    }
    
    std::string msg = "";
    for(int i=0; i<g_blocksize; ++i)
    {
        msg.push_back('h');
    }

    std::vector<ConClient*> vec_conClient;
    for(int i=0; i<g_con_counts; ++i)
    {
        ConClient* conClient = new ConClient(&g_reactor_engine);//复用引擎
        vec_conClient.push_back(conClient);
    }
    
    CallbackHandler* handler = new CallbackHandler(&g_reactor_engine);
    g_reactor_engine.init_reactor(handler, 4);
    
    for(int i=0; i<g_con_counts; ++i)
    {
        int ret = vec_conClient[i]->startConnect(ip, port);
        if(ret > 0)
            vec_conClient[i]->send_data(msg.c_str(), (int)msg.length());
    }

	char c;
	do{
		c = getchar();
		SLEEP_MS(100);

		printf("Press q or Q to exit...\n");
	}while(c!='q' && c!='Q');

    for(int i=0; i<g_con_counts; ++i)
    {
        SAFE_DELETE(vec_conClient[i]);
    }
    vec_conClient.clear();
	g_reactor_engine.uninit_reactor();
    SAFE_DELETE(handler);
	return 0;
}


